TechAid24

Your Technical Consultant

The Only Redis Cheat Sheet You’ll Ever Need – Beginner to Advanced

Posted on by Anonymous Guest 6 minutes read

The Redis cheat sheet includes basic syntax and methods to help you use Redis. Redis is an open-source (BSD licensed), in-memory data structure store used as a database, cache, message broker, and streaming engine. Redis provides data structures such as strings, hashes, lists, sets, sorted sets with range queries, bitmaps, hyperloglogs, geospatial indexes, and streams.

Redis has built-in replication, Lua scripting, LRU eviction, transactions, and different levels of on-disk persistence, and provides high availability via Redis Sentinel and automatic partitioning with Redis Cluster.

 

String

Redis strings store sequences of bytes, including text, serialized objects, and binary arrays. As such, strings are the simplest type of value you can associate with a Redis key. They’re often used for caching, but they support additional functionality that lets you implement counters and perform bitwise operations, too.

APPENDAppend
BITCOUNTCount set bits
BITOPBitwise operations
BITPOSFind the first set of bit
DECRDecrement integer
DECRBYSubtract from integer
GETGet by key
GETBITGet bit by index
GETRANGEGet substring
GETSETSet, returning the old value
INCRIncrement integer
INCRBYAdd to integer
INCRBYFLOATAdd to float
MGETGet multiple
MSETSet multiple
MSETNXSet multiple if don’t exist
PSETEXSet with expiry (ms)
SETSet
SETBITSet bit by index
SETEXSet with expiry (seconds)
SETNXSet if doesn’t exist
SETRANGESet substring
STRLENGet length
Strings can be used as numbers, arrays, bit sets, and binary data

 

Lists

Redis lists are implemented via Linked Lists. This means that even if you have millions of elements inside a list, the operation of adding a new element in the head or in the tail of the list is performed in constant time.

BLPOPBlocking left pop
BRPOPBlocking right pop
BRPOPLPUSHBlocking rotate
LINDEXAccess by index
LINSERTInsert next to
LLENGet length
LPOPPop from start
LPUSHPush onto start
LPUSHXPush if the list exists
LRANGEAccess Range
LREMRemove
LSETSet item by index
LTRIMRemove start and/or end items
RPOPPop from end
RPOPLPUSHRotate
RPUSHPush onto end
RPUSHXPush onto the end if a list exists

 

Hashes

Redis hashes are record types structured as collections of field-value pairs. You can use hashes to represent basic objects and to store groupings of counters, among other things.

HDELDelete item
HEXISTSCheck for item
HGETGet item
HGETALLReturn all items
HINCRBYAdd to the integer value
HINCRBYFLOATAdd to float value
HKEYSReturn all keys
HLENGet the number of items
HMGETGet multiple items
HMSETSet multiple items
HSCANIterate items
HSETSet item
HSETNXSet item if doesn’t exist
HVALSReturn all values

 

Sets

A Redis set is an unordered collection of unique strings (members). You can use Redis sets to efficiently:

  • Track unique items (e.g., track all unique IP addresses accessing a given blog post).
  • Represent relations (e.g., the set of all users with a given role).
  • Perform common set operations such as intersection, unions, and differences.
SADDAdd item
SCARDGet size
SDIFFGet difference
SDIFFSTOREStore difference
SINTERInters­ection
SINTERSTOREStore inters­ection
SISMEMBERCheck for item
SMEMBERSGet all
SMOVEMove item to another set
SPOPPop random item
SRANDMEMBERGet random item
SREMRemove matching
SSCANIterate items
SUNIONUnion
SUNIONSTOREStore union

 

Sorted sets

A Redis sorted set is a collection of unique strings (members) ordered by an associated score. When more than one string has the same score, the strings are ordered lexicographically. Some use cases for sorted sets include:

ZADDAdd item
ZCARDGet number of items
ZCOUNTNumber of items within score range
ZINCRBYAdd to score
ZINTERSTOREStore inters­ection
ZLEXCOUNTLexico­gra­phical range count
ZRANGEGet items within rank range
ZLEXRANGEGet items within lexico­gra­phical range
ZRANGEBYSCOREGet items within score range
ZRANKGet item rank
ZREMRemove item(s)
ZREMRANGEBYLEXRemove items within lexico­gra­phical range
ZREMRANGEBYRANKRemove items within rank range
ZREMRANGEBYSCORERemove items within score range
ZREVRANGEZRANGE in reverse order
ZREVRANGEBYSCOREZRANGE­BYSCORE in reverse order
ZREVRANKZRANK in reverse order
ZSCANIterate items
ZSCOREGet item score
ZUNIONSTOREStore union
Lexico­gra­phical commands require all items to have the same score

 

Redis Streams

A Redis stream is a data structure that acts like an append-only log but also implements several operations to overcome some of the limits of a typical append-only log. These include random access in O(1) time and complex consumption strategies, such as consumer groups. You can use streams to record and simultaneously syndicate events in real time.

XADDAdds a new entry to a stream.
XREADReads one or more entries, starting at a given position and moving forward in time.
XRANGEReturns a range of entries between two supplied entry IDs.
XLENReturns the length of a stream.

 

Redis Geospatial

Redis geospatial indexes let you store coordinates and search for them. This data structure is useful for finding nearby points within a given radius or bounding box.

GEOADDAdds a location to a given geospatial index (note that longitude comes before latitude with this command).
GEOSEARCHReturns locations with a given radius or a bounding box.

 

Redis Bitmaps

Bitmaps are not an actual data type, but a set of bit-oriented operations defined on the String type which is treated like a bit vector. Since strings are binary safe blobs and their maximum length is 512 MB, they are suitable to set up to 2^32 different bits.

SETBITSets a bit at the provided offset to 0 or 1.
GETBITReturns the value of a bit at a given offset.
BITOPLets you perform bitwise operations against one or more strings.

 

Redis Bitfields

Redis bitfields let you set, increment, and get integer values of arbitrary bit length. For example, you can operate on anything from unsigned 1-bit integers to signed 63-bit integers. These values are stored using binary-encoded Redis strings. Bitfields support atomic read, write and increment operations, making them a good choice for managing counters and similar numerical values.

BITFIELDAtomically sets, increments and reads one or more values.
BITFIELD_ROA read-only variant of BITFIELD

 

Database
DELDelete item
DUMPSerialise item
EXISTSCheck for key
EXPIRESet a timeout on item
EXPIREATSet timeout by timestamp
KEYSGet all keys to match the pattern
MIGRATETransfer an item between Redis instances
MOVETransfer an item between databases
OBJECTInspect item
PERSISTRemove timeout
PEXPIRESet timeout (ms)
PEXPIREATSet timeout (ms timestamp)
PTTLGet item time to live (ms)
RANDOMKEYGet random key
RENAMEChange item’s key
RENAMENXChange item’s key if new key doesn’t exist
RESTOREDeseri­alise
SCANIterate keys
SORTGet or store sorted copy of list, set or sorted set
TTLGet item time to live
TYPEGet type of item

 

Client­/Server
AUTHRequest authen­tic­ation
ECHOReturn message
PINGTest connection
QUITClose connection
SELECTSet current database by index

 

Scripts
EVALRun
EVALSHARun cached
SCRIPT EXISTSCheck by hash
SCRIPT FLUSHClear cache
SCRIPT KILLKill running script
SCRIPT LOADAdd to cache
Lua scripts access keys through the array KEYS and additional arguments through the array ARGV.

 

HyperL­ogLogs
PFADDAdd items
PFCOUNTGet approx­imate size
PFMERGEMerge HyperL­ogLogs
Redis Cheat Sheet

Tags:

Anonymous Guest

An anonymous author is a writer who conceals their true identity when publishing content, often for privacy, security, or confidentiality reasons. This approach is often used in technical articles to focus on the information being presented rather than the author's personal identity.

Leave a Reply

Your email address will not be published. Required fields are marked *